package com.hr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hr.entity.AoaInMailList;
import com.hr.entity.AoaMailnumber;
import com.hr.mapper.AoaInMailListMapper;
import com.hr.mapper.AoaMailReciverMapper;
import com.hr.mapper.AoaMailnumberMapper;
import com.hr.service.AoaMailnumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.Set;

/**
 *  邮箱服务
 * (AoaMailnumber)表服务实现类
 *
 * @author makejava
 * @since 2022-06-16 11:44:31
 */
@Service
//* @Transactional Propagation.REQUIRED:支持当前事务，如果当前没有事务，就新建一个事务。
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public class AoaMailnumberServiceImpl implements AoaMailnumberService {
    @Autowired
    private AoaMailnumberMapper aoaMailnumberMapper;
    @Autowired
    private AoaInMailListMapper aoaInMailListMapper;
    @Autowired
    private AoaMailReciverMapper aoaMailReciverMapper;
    /**
     * 分页查询
     */
    @Override
    public Page<AoaMailnumber> queryByPage(Integer current,String name) {
        QueryWrapper<AoaMailnumber> wrapper = new QueryWrapper();
        if(name!=null){
            wrapper.like("mail_user_name",name);
        }
        Page<AoaMailnumber> page = new Page<>();
        page.setCurrent(current);
        return aoaMailnumberMapper.selectPage(page, wrapper);
    }

    /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
    @Override
    public AoaMailnumber queryOne(Long id) {
        AoaMailnumber aoaMailnumber=new AoaMailnumber();
        aoaMailnumber.setMailNumberId(id);
        QueryWrapper<AoaMailnumber> wrapper = new QueryWrapper<>();
        wrapper.eq("Mail_Number_Id",id);
        return  aoaMailnumberMapper.selectOne(wrapper);
    }

    /**
     * 修改用户
     * @param mailnumber AoaMailnumber
     * @return
     */

    public ResponseEntity<String> editMailnumber(AoaMailnumber mailnumber){
        int i = aoaMailnumberMapper.updateById(mailnumber);
        if(i>=1){
            return ResponseEntity.ok("修改成功");
        }else
            return ResponseEntity.ok("失败");
    }
    /**
     * 删除数据
     */
    public ResponseEntity deleteData(String id){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("mail_number_id",id);
        int i=  aoaInMailListMapper.delete(wrapper);
        int i_2 = aoaMailnumberMapper.deleteById(id);
        if(i+i_2>=2){
            return ResponseEntity.ok("删除成功");
        }else
            return ResponseEntity.ok("失败");
    }

    /**
     * 添加数据
     * @param aoaMailnumber AoaMailnumber
     * @return
     */
    public ResponseEntity saveData(AoaMailnumber aoaMailnumber){
        return ResponseEntity.ok(aoaMailnumberMapper.insert(aoaMailnumber));
    }

    /**
     * 根据用户ID查询邮箱内部消息列表
     */
    public Page<AoaInMailList> getMailList(Long id,Integer current){
        //查詢用戶對於aoa_mailnumber的mail_number_id
        //分頁查詢對於用戶的郵箱內部信息
        QueryWrapper<AoaInMailList> wrapper = new QueryWrapper<>();
        wrapper.in("mail_number_id",aoaInMailListMapper.queryMailList(id));
        wrapper.eq("mail_del",1);
        Page<AoaInMailList> page = new Page<>();
        page.setSize(10);
        page.setCurrent(current);
        return aoaInMailListMapper.selectPage(page,wrapper);
    }
    /**
     * 根据id列表删除数据
     */
    public ResponseEntity delMail(Integer[] arr){
        Set<Integer> set = new HashSet<>();
        for (Integer ar:arr) {
            set.add(ar);
        }
        int i = aoaInMailListMapper.del(set);
        if(i>1){
             return ResponseEntity.ok("删除成功");
        }else{
            return ResponseEntity.ok("删除失败");
        }
    }
    /**
     * 標記已讀
     */
    public ResponseEntity asterisk(Integer[] arr){
        Set<Integer> set = new HashSet<>();
        for (Integer ar:arr) {
            set.add(ar);
        }
        Integer integer = aoaInMailListMapper.asterisk(set);
        if(integer>0){
            return ResponseEntity.ok("成功");
        }
        return ResponseEntity.ok("出了點差錯");
    }
}
